package com.apes.fn.fico.expenseType;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;


/***
 * 定义费用类型
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ExpenseType.CONST_TABLE_NAME)
public class ExpenseType extends Model {
    public static final String CONST_TABLE_NAME = "fn_expense_type";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "费用", padding = 8)
    @Comment("费用类型编码")
    private String id;

    @Name
    @Column(name = "name", nullable = false)
    @Comment("费用类型名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "period_type", referencedColumnName = "id")
    @Comment("周期类型")
    private Enumeration periodType; //(单次;每月;每季度;每半年;每年);

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "calculate_base", referencedColumnName = "id")
    @Comment("计算基数")
    private Enumeration calculateBase; //(无;合同保证金;合同定金;销售额;保险佣金扣点;技术支援天数;代购佣金费率);


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "calculate_method", referencedColumnName = "id")
    @Comment("计算方法")
    private Enumeration calculateMethod; //(固定金额; 扣点; 单次费用);


    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率编码")
    private Tax tax;


    @Column(name = "valid", nullable = false, columnDefinition = "number(1, 0) default 1")
    @Comment("有效标记")
    private boolean valid; // 有效标记

    @Column(name = "invoice_valid", nullable = false, columnDefinition = "number(1, 0) default 0")
    @Comment("是否开票")
    private boolean invoiceValid;

    @Name
    @Column(name = "remark", length = 120)
    @Comment("备注")
    private String remark;


    @Transient
    private Party _creator;

    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

    /***
     * 验证范围的合法性
     * @return
     */
    public boolean isRangeValid(double qty) {
        if( calculateMethod.getCode().equals("fixedAmount") ){
            return qty != 0.00;
        } else if (calculateMethod.getCode().equals("point")) {
            return qty != 0.00 && qty > -1 &&  qty < 1;
        } else if (calculateMethod.getCode().equals("once")){
            return qty != 0.00;
        }
        throw new RuntimeException("未定义对应的规则,计算方法为:" +  calculateMethod.getDescription());
    }
}
